package cn.jinjimi.app.main.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.Selection;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.UserController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.InfoUpdateEvent;
import cn.jinjimi.app.controller.event.UploadProfileEvent;
import cn.jinjimi.app.entity.Account;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by m1 on 15/1/26.
 */
public class ModifyRealNameActivity extends ToolBarCommonActivity {

    private EditText realnameEditText;
    private ImageButton cleanButton;

    public static void launchMeForResult(Context startActivity,int requestCode){
        if(startActivity==null)
            return;
        Intent intent = new Intent(startActivity, ModifyRealNameActivity.class);
        if (!(startActivity instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        ((Activity) startActivity).startActivityForResult(intent, requestCode);
    }

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_modify_realname);
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        setTitle(getString(R.string.info_realname));
        realnameEditText = (EditText) findViewById(R.id.et_real_name);
        cleanButton = (ImageButton) findViewById(R.id.clean_btn);
        setRightText(R.string.save);
    }

    protected void initListener() {
        cleanButton.setOnClickListener(this);
    }

    protected void initData() {
        Account account = AccountManager.getInstance().getAccount();
        realnameEditText.setText(account.realName);
        //光标移到句末
        Editable etext = realnameEditText.getText();
        Selection.setSelection(etext, etext.length());
    }

    public void saveRealName(){
        if (!AccountManager.getInstance().getAccount().realName.equals(realnameEditText.getText().toString())) {
            showLoadingIndicator();
            UserController.setRealName(realnameEditText.getText().toString().trim());
        }
    }

    @Override
    protected void rightAction() {
        super.rightAction();
        if (StringUtils.isChineseName(this, realnameEditText))
            saveRealName();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.clean_btn:
                realnameEditText.setText("");
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUploadProfileEvent(UploadProfileEvent event) {
        if (isFinishing())
            return;
        switch (event.state) {
            case DataEvent.SUCC:
                AccountManager.getInstance().updateAccount(AccountManager.NameKey.REALNAME,
                        realnameEditText.getText().toString().trim());

                ToastUtils.toastMessage(ModifyRealNameActivity.this, getString(R.string.modify_success));
                setResult(RESULT_OK);
                EventBus.getDefault().post(new InfoUpdateEvent());
                finish();
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(ModifyRealNameActivity.this, getString(R.string.modify_fail));
                break;
        }
        hideLoadingIndicator();
    }

}
